package pub.gll.onepeas.todo.gradle

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch

val timerFlow = flow {
    val start = 0
    var current = start
    while (true) {
        emit(current)
        current++
        delay(1000)
    }
}

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        var firstTimer by mutableStateOf(0)
        var secondTimer by mutableStateOf(0)
        var thirdTimer by mutableStateOf(0)
        val fontSize: TextUnit = 30.sp
        lifecycleScope.launch {
            while (true) {
                delay(1000)
                firstTimer++
            }
        }
        setContent {
            var secondTimerIsVisible by remember {
                mutableStateOf(false)
            }
            var thirdTimerIsVisible by remember {
                mutableStateOf(false)
            }
            Column(
                modifier = Modifier.fillMaxSize(),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                Text(
                    text = "屏幕启动时间为${firstTimer}秒",
                    textAlign = TextAlign.Center, fontSize = fontSize
                )
                if (secondTimerIsVisible) {
                    Text(
                        "第一个自定义计时器的时间为${secondTimer}秒。",
                        textAlign = TextAlign.Center,
                        fontSize = fontSize
                    )
                } else {
                    Button(
                        onClick = {
                            lifecycleScope.launch {
                                repeatOnLifecycle(Lifecycle.State.STARTED) {
                                    timerFlow.collect {
                                        secondTimer = it
                                    }
                                }
                            }
                            secondTimerIsVisible = true
                        },
                    ) {
                        Text(
                            text = "启动第一个自定义计时器",
                            textAlign = TextAlign.Center,
                            fontSize = fontSize
                        )
                    }
                }
                if (thirdTimerIsVisible) {
                    Text(
                        "第二个自定义计时器的时间为${thirdTimer}秒。",
                        textAlign = TextAlign.Center,
                        fontSize = fontSize
                    )
                } else {
                    Button(
                        modifier = Modifier.padding(10.dp),
                        onClick = {
                            lifecycleScope.launch {
                                repeatOnLifecycle(Lifecycle.State.STARTED) {
                                    timerFlow.collect {
                                        thirdTimer = it
                                    }
                                }
                            }
                            thirdTimerIsVisible = true
                        },
                    ) {
                        Text(
                            text = "启动第二个自定义计时器",
                            textAlign = TextAlign.Center,
                            fontSize = fontSize
                        )
                    }
                }
            }
        }
    }
}